"""
AI分析结果数据模型
"""
from sqlalchemy import Column, String, Integer, ForeignKey, JSON, Float, DateTime, Text, Boolean
from sqlalchemy.orm import relationship
from models.base import BaseModel


class AIResult(BaseModel):
    """AI分析结果模型"""
    __tablename__ = "ai_results"
    
    task_id = Column(Integer, ForeignKey("ai_tasks.id"), nullable=True, comment="任务ID")
    camera_id = Column(Integer, ForeignKey("cameras.id"), nullable=False, comment="摄像头ID")
    algorithm_name = Column(String(100), nullable=False, comment="算法名称")
    result_type = Column(String(50), nullable=False, comment="结果类型")
    result_data = Column(JSON, nullable=False, comment="分析结果数据")
    confidence = Column(Float, comment="置信度")
    bounding_boxes = Column(JSON, comment="边界框数据")
    timestamp = Column(DateTime, nullable=False, comment="分析时间戳")
    processed_at = Column(DateTime, comment="处理完成时间")
    extra_data = Column(JSON, comment="额外数据")
    
    # 实时处理相关字段
    frame_timestamp = Column(DateTime, comment="视频帧时间戳")
    processing_latency = Column(Float, comment="处理延迟(ms)")
    frame_id = Column(String(100), comment="帧ID")
    stream_info = Column(JSON, comment="流信息")
    is_real_time = Column(Boolean, default=False, comment="是否实时处理")
    batch_id = Column(String(100), comment="批处理ID")
    worker_id = Column(String(50), comment="处理工作器ID")
    gpu_id = Column(Integer, comment="使用的GPU ID")
    frame_size = Column(JSON, comment="帧尺寸信息")
    alert_triggered = Column(Boolean, default=False, comment="是否触发告警")
    
    # 关联关系
    task = relationship("AITask", back_populates="results")
    camera = relationship("Camera", back_populates="ai_results")
    alerts = relationship("Alert", back_populates="ai_result")
    
    def __repr__(self):
        return f"<AIResult(id={self.id}, type='{self.result_type}', confidence={self.confidence})>"